<template>
  <el-container>
    <my-theme-card>
      <el-header />
      <el-container>
        <el-main>
          <el-col :span="1"><div class="grid-content bg-purple-light">
            <el-button class="table-header-btn" type="primary" icon="el-icon-plus" size="mini" @click="addClick">新增</el-button>
          </div></el-col>
          <template>
            <el-table :data="notes" stripe border style="width: 100%">
              <el-table-column prop="name" label="笔记本名称" width="180" />
              <el-table-column prop="create_time" label="创建日期" />
              <el-table-column label="是否发布" width="180">
                <template slot-scope="scope">
                  <el-switch
                    v-model="scope.row.release"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                  />
                </template>
              </el-table-column>
              <el-table-column
                fixed="right"
                label="操作"
                width="180"
              >
                <template slot-scope="scope">
                  <el-button type="text" size="small" @click="editClick(scope.row)">编辑</el-button>
                  <el-button type="text" size="small" @click="deleteClick(scope.row)">删除</el-button>
                </template>
              </el-table-column>

            </el-table>
          </template>
        </el-main>
      </el-container>

      <el-footer>
        <div class="block">
          <el-pagination
            :current-page="page.current"
            :page-sizes="[5,10, 20, 50, 100]"
            :page-size="page.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-footer>
      <el-dialog
        title="删除确认"
        :visible.sync="dialogConfirmVisible"
        :close-on-click-modal="false"
        width="30%"
        append-to-body
        center
      >
        <span>{{ deleteMessage }}</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogConfirmVisible = false">取 消</el-button>
          <el-button type="danger" @click="deleteNoteTask">确 定</el-button>
        </span>
      </el-dialog>
    </my-theme-card>
  </el-container>
</template>

<script>
import request from '@/utils/request'
export default {
  data() {
    return {
      page: {
        current: 1,
        size: 5,
        total: 0
      },
      notes: [],
      deleteNote: {},
      deleteMessage: '确认删除该笔记吗？',
      dialogConfirmVisible: false
    }
  },
  computed: {
    key_logic: function() {
      const { logic = {}} = this.$store.state.directory.directoryItem
      return logic
    },
    dir_logic: function() {
      return this.$store.state.directory.directory['logic']
    }
  },
  created() {
    this.directoryUpdate()
    this.query()
  },
  methods: {
    directoryUpdate: function() {
      this.$store.commit('directory/setDirectory', 'logic')
    },
    // 每页显示数目发生变化时
    handleSizeChange: function(val) {
      this.page.size = val
      this.page.current = 1
      this.query()
    },
    // 当前页发生变化时
    handleCurrentChange: function(val) {
      this.page.current = val
      this.query()
    },
    editClick: function(row) {
      this.$emit('toChangePage', 'edit', row)
    },
    // 添加事件
    addClick: function() {
      this.$emit('toChangePage', 'add')
    },
    // 删除事件
    deleteClick: function(note) {
      this.dialogConfirmVisible = true
      this.deleteNote = note
    },
    // 删除笔记任务
    deleteNoteTask: function() {
      this.$progress.start()
      request({
        url: '/notebook/note/delete',
        method: 'post',
        data: this.deleteNote
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.dialogConfirmVisible = false
          this.query()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    },
    // 查询所有笔记
    query: function() {
      this.$progress.start()
      request({
        url: '/notebook/note/query',
        method: 'post',
        data: this.page
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.page.total = res.data.total
          this.notes = res.data.data
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    }
  }
}
</script>
